package com.example.demo.controller;

import com.example.demo.bean.BookBean;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class ElasticController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/bookById/{id}")
    @ResponseBody
    public BookBean getBookById(@PathVariable String id){
        Optional<BookBean> opt =bookService.findById(id);
        BookBean book=opt.get();
        System.out.println(book);
        return book;
    }

    @RequestMapping("/bookByTitle/{title}")
    @ResponseBody
    public List<BookBean> getBookByTitle(@PathVariable String title){
        Iterable iterable = bookService.queryBooks(title);
        List<BookBean> list =new ArrayList<>();
        iterable.forEach(p->{
            list.add((BookBean)p);
        });

        System.out.println(list.toString());
        return list;
    }

    @RequestMapping("/save")
    @ResponseBody
    public void Save(){
        BookBean book=new BookBean("1","ES入门教程","mdl","2020-03-26");
        System.out.println(book);
        bookService.save(book);
    }

}


